
function initialize()
{
	var url = new ArgumentURL();
	var arg;

	arg = url.getArgument("business");
	document.title = arg;
	if (arg != undefined && arg != "undefined"){
		document.getElementById("BusName").appendChild(document.createTextNode(arg));
	}
	
	arg = url.getArgument("telephone");
	if (arg != undefined && arg != "undefined"){
		document.getElementById("BusNum").appendChild(document.createTextNode(arg));
	}
	
	arg = url.getArgument("cell");
	if (arg != undefined && arg != "undefined"){
		document.getElementById("BusFax").appendChild(document.createTextNode(arg));
	}
	
	arg = url.getArgument("city");
	if (arg != undefined && arg != "undefined"){
		document.getElementById("BusCity").appendChild(document.createTextNode(arg));
	}
	
	arg = url.getArgument("email");
	if (arg != undefined && arg != "undefined"){
		document.getElementById("BusEmail").appendChild(document.createTextNode(arg));
	}
	
	arg = url.getArgument("fax");
	if (arg != undefined && arg != "undefined"){
		document.getElementById("BusFax").appendChild(document.createTextNode(arg));
	}
	
	arg = url.getArgument("postal");
	if (arg != undefined && arg != "undefined"){
		document.getElementById("BusPost").appendChild(document.createTextNode(arg));
	}
	
	arg = url.getArgument("province");
	if (arg != undefined && arg != "undefined"){
		document.getElementById("BusProv").appendChild(document.createTextNode(arg));
	}
	
	arg = url.getArgument("address");
	if (arg != undefined && arg != "undefined"){
		document.getElementById("BusAdd").appendChild(document.createTextNode(arg));
	}
	
	getReviews();
}

function postReviews()
{
	
	var poster = document.getElementById("posterName").value;
	var password = document.getElementById("posterPass").value;
	var email = document.getElementById("posterEmail").value;

	$.getJSON('/GoGoGadget/SubLogin?userName=' + poster + '&password=' + password + '&email=' + email, reviewLoggedIn);
}


function reviewLoggedIn()
{
	var content = document.getElementById("content").value;
	var url = new ArgumentURL();
	var arg = url.getArgument("business");
	
	if (arg != undefined && arg != "undefined"){
	$.getJSON('/GoGoGadget/PostReviewServlet?content=' + content
														+ '&business=' + arg);
	}
	getReviews();
}


function addReviewer()
{
	
	var user = document.getElementById("userName").value;
	var pass = document.getElementById("password").value;
	var email = document.getElementById("email").value;
	
	$.getJSON('/GoGoGadget/AddReviewerServlet?userName=' + user
												+ '&password=' + pass
												+ '&email=' + email, subscribedone);
}

function subscribeTo()
{	
	var url = new ArgumentURL();
	var arg = url.getArgument("business");
	
	if (arg != undefined && arg != "undefined"){
		$.getJSON('/GoGoGadget/SubToServlet?busName=' + arg);
	}
}

function subscribedone()
{
	window.location= 'http://localhost:8080/GoGoGadget/mySubscriptions.jsp';
}

function getSubscriptions()
{

	$.getJSON('/GoGoGadget/SubInfoServlet', infoSet);
	
	$.getJSON('/GoGoGadget/SubscriptionServlet', displaySubs);
}

function removeSubscription()
{
	var business = document.getElementById("deletSub").value;
	
	$.getJSON('/GoGoGadget/RemoveSubscriptionServlet?busName=' + business, subscribedone);
}

function removeSubUser()
{
	
	$.getJSON('/GoGoGadget/DeleteSubServlet', subscribedone);
}

function displaySubs(data)
{
	clearSubsContent();
	
	var index;
	var subList = data.SubList;
	var list = document.getElementById("talk");
	
	for (index=0; index<subList.length; index++) {
		var listElement = document.createElement("li");
		var sub = subList[index];
		listElement.appendChild(document.createTextNode(sub));
		listElement.setAttribute("subscription", sub);
		
		list.appendChild(listElement);
	}
	
	
}

function subLogin()
{	
	var user = document.getElementById("username").value;
	var pass = document.getElementById("password").value;
	var email = document.getElementById("email").value;
	
	$.getJSON('/GoGoGadget/SubLogin?userName=' + user + '&password=' + pass + '&email=' + email, subscribedone);
}

function subLogout()
{
	$.getJSON('/GoGoGadget/SubLogoutServlet', subscribedone);
}

function editSub()
{
	var newPass = document.getElementById("editSubPass").value;
	var oldPass = document.getElementById("subPass").value;
	var email = document.getElementById("editSubEmail").value;
	
	$.getJSON('/GoGoGadget/SubEditInfo?subEmail='+ email + '&newPass='+ newPass + '&oldPass='+ oldPass, subscribedone);
}

function infoSet(data)
{
	
	var name = data.SubUserName;
	//var password = data.SubPass;
	var email = data.SubEmail;
	
	var subUser = document.getElementById("subUserName");
	//var subPass = document.getElementById("subPass");
	var subEmail = document.getElementById("subEmail");
	
	document.getElementById("username").value="";
	document.getElementById("password").value="";
	document.getElementById("email").value="";
	
	subUser.appendChild(document.createTextNode(name));
	subEmail.appendChild(document.createTextNode(email));
	//subPass.appendChild(document.createTextNode(password));
	
}

function getReviews()
{
	
	var reviewcontent = document.getElementById("reviewcontent");
	var url = new ArgumentURL();
	var arg;

	arg = url.getArgument("business");
	document.title = arg;
	if (arg != undefined && arg != "undefined"){
		$.getJSON('/GoGoGadget/ReviewServlet?business=' + arg + '&number=' + 
				reviewcontent.displayNum, displayReviews);
	}
	clearReviewInfo();
}

function getBusReviews()
{

	$.getJSON('/GoGoGadget/ReviewBusServlet', displayReviews);
}

function displayReviews(data)
{
	
	
	var reviews = data.reviews;
	
	var num;
	var offset;
	
	offset = reviewcontent.start*reviewcontent.displayNum;

	clearReviewContent();

	var table = document.createElement("table");
	
	table.setAttribute("id", "reviewtable");
	table.setAttribute("width", "100%");
	table.setAttribute("border", "1");
	table.setAttribute("cellpadding", "0");
	table.setAttribute("cellspacing", "0");
	
	var sendHead = document.createElement("th");
	sendHead.appendChild(document.createTextNode("poster"));
	
	var subHead = document.createElement("th");
	subHead.appendChild(document.createTextNode("content"));
	
	table.appendChild(sendHead);
	table.appendChild(subHead);
	
	
	for (num = 0; num<reviews.length; num++) {

		var review = reviews[num];
		var row = document.createElement("tr");
		
		row.setAttribute("index", offset + num+1);

		var poster = document.createElement("td");
		poster.setAttribute("id", "poster");
		var senderData = document.createTextNode(review.poster);
		poster.appendChild(senderData);
		
		var content = document.createElement("td");
		content.setAttribute("id", "content");
		var subjectData = document.createTextNode(review.content);
		content.appendChild(subjectData);
		
		row.appendChild(poster);
		row.appendChild(content);
		table.appendChild(row);
	}
	
	reviewcontent.appendChild(table);
}


function clearReviewContent()
{	
	var reviewContent = document.getElementById("reviewcontent");

	if ( reviewContent.hasChildNodes() )
	{
	    while ( reviewContent.childNodes.length >= 1 )
	    {
	    	reviewContent.removeChild( reviewContent.firstChild );       
	    } 
	}
}

function clearReviewInfo()
{
	
	document.getElementById("posterName").value="";
	document.getElementById("posterEmail").value="";
	document.getElementById("posterPass").value="";
	document.getElementById("content").value="";
	
}

function clearcont()
{
	document.getElementById("content").value="";
	
}

function clearSubsContent()
{
	var subContent = document.getElementById("talk");

	if ( subContent.hasChildNodes() )
	{
	    while ( subContent.childNodes.length >= 1 )
	    {
	    	subContent.removeChild( subContent.firstChild );       
	    } 
	}
}

function ArgumentURL() {
	this.getArgument = _getArg;
	this.setArgument = _setArg;
	this.removeArgument = _removeArg;
	this.toString    = _toString;	//Allows the object to be printed
					//no need to write toString()
	this.arguments   = new Array();

	// Initiation
	var separator = "&";
	var equalsign = "=";
	
	var str = window.location.search.replace(/%20/g, " ");
	var index = str.indexOf("?");
	var sInfo;
	var infoArray = new Array();

	var tmp;
	
	if (index != -1) {
		sInfo = str.substring(index+1,str.length);
		infoArray = sInfo.split(separator);
	}

	for (var i=0; i<infoArray.length; i++) {
		tmp = infoArray[i].split(equalsign);
		if (tmp[0] != "") {
			var t = tmp[0];
			this.arguments[tmp[0]] = new Object();
			this.arguments[tmp[0]].value = tmp[1];
			this.arguments[tmp[0]].name = tmp[0];
		}
	}
	

	
	function _toString() {
		var s = "";
		var once = true;
		for (i in this.arguments) {
			if (once) {
				s += "?";
				once = false;
			}
			s += this.arguments[i].name;
			s += equalsign;
			s += this.arguments[i].value;
			s += separator;
		}
		return s.replace(/ /g, "%20");
	}
	
	function _getArg(name) {
		if(this.arguments[name] == undefined) {
			return null;
		}
		
		if (typeof(this.arguments[name].name) != "string") {
			return null;
		}
		else {
			return this.arguments[name].value;
		}
	}
	
	function _setArg(name,value) {
		this.arguments[name] = new Object();
		this.arguments[name].name = name;
		this.arguments[name].value = value;
	}
	
	function _removeArg(name) {
		this.arguments[name] = null;
	}
	
	return this;
}
